﻿using AutoMapper;
using MediatR;
using MyRabbit.Application.Command.ImmuneCommand;
using MyRabbit.Application.ErrorCode;
using MyRabbit.Domain;
using MyRabbit.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyRabbit.Application.CommandHandler.ImmuneCommandHandler
{
    public class ImmuneUpdateCommandHandler : IRequestHandler<ImmuneUpdateCommand, APIResult<object>>
    {
        private readonly IRepository<Immune> immRep;
        private readonly IMapper mapper;
        public ImmuneUpdateCommandHandler(IRepository<Immune> immRep, IMapper mapper)
        {
            this.immRep = immRep;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(ImmuneUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = Codes.修改成功;
            result.Message = Codes.修改成功.ToString();
            try
            {
                Immune immune = await immRep.GetByIdAsync(request.ImmuneId);
                immune.FeedingDays = request.FeedingDays;
                immune.ImmunityType = request.ImmunityType;
                immune.ImmunityMethod = request.ImmunityMethod;

                await immRep.UpdateAsync(immune);
            }
            catch (Exception ex)
            {
                result.Code = Codes.修改失败;
                result.Message = Codes.修改失败 + ex.Message;
                return await Task.FromResult(result);
            }
            return await Task.FromResult(result);
        }
    }
}
